package com.ruoyi.partybuilding.controller;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.partybuilding.domain.vo.HonorVo;
import com.ruoyi.partybuilding.service.impl.OrgServiceImpl;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.var;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.domain.Honor;
//import com.ruoyi.partybuilding.service.HonorService;
import com.ruoyi.partybuilding.common.*;

/**honor
 * 荣誉管理;(pb_honor)表控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2024-12-5
 */
@Api(tags = "荣誉管理 功能接口")
@RestController
@RequestMapping("/partybuilding/Honor")
public class HonorController extends EQueryController<Honor>{

    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:query')")
    @GetMapping(value = "/{id}")
    public R<Honor> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }
    @ApiOperation("分页")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:list')")
    @GetMapping("query")
    public R<Page<Honor>> query(HonorVo entity, PageDomain pageRequest) {

        return super.queryPage(entity, pageRequest);
    }
    @ApiOperation("列表,对query的rouyi兼容包装")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:list')")
    @GetMapping("list")
    public TableDataInfo list(HonorVo entity, PageDomain pageRequest) {
        return super.list(entity,pageRequest);
    }
    @Resource
    private ISysUserService userService;

    @Resource
    OrgServiceImpl orgService;
    @ApiOperation(value = "新增",notes = "userIds批量个人荣誉添加使用，优先级高于userId，为空则按照单个添加，不为空则忽略userId")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:add')")
    @Log(title = "荣誉管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R<Long> add(@RequestBody HonorVo vo) {


        if (vo.getUserIds() == null || vo.getUserIds().length < 1) {
            var orgRe = orgService.deptVoGet(vo.getDeptId());
            if (orgRe.getData() == null)
                return R.fail("部门错误");
            vo.setDeptName(orgRe.getData().getDeptName());
            return super.add(vo);
        }
        var list=new ArrayList<Honor>();
        for (Long id : vo.getUserIds())
        {
           var user= userService.selectUserById(id);
           var newEntity=new Honor();
            BeanUtils.copyProperties(vo,newEntity);
            newEntity.setId(null);
            newEntity.setUserId(id+"");
            newEntity.setUserName(user.getNickName());
            if(user.getDeptId()==null)
                return R.fail("用户部门为空");
            newEntity.setDeptId(user.getDeptId());
            newEntity.setDeptName(user.getDept().getDeptName());
            list.add(newEntity);
        }
        var count=queryHelper.query().insertable(list).executeRows();
        return R.ok(count);
    }
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:remove')")
    @Log(title = "荣誉管理", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:edit')")
    @Log(title = "荣誉管理", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  R<Long> edit(@RequestBody Honor entity){
        return super.edit(entity);
    }
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('partybuilding:Honor:export')")
    @Log(title = "荣誉管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Honor entity, PageDomain pageRequest,HttpServletResponse response) {
        pageRequest.setPageSize(Integer.MAX_VALUE);
        var rows = list(entity, pageRequest);
        var util = new ExcelUtil<>(tClass);

        if(entity.getTeamHonor()==0)
        {
            util.hideColumn("typeOrg");
            util.exportExcel(response, rows.getRows(), "个人荣誉数据_" + DateUtil.today());
            return;
        }
        util.hideColumn("typePerson","userName");
        util.exportExcel(response, rows.getRows(), "组织荣誉数据_" + DateUtil.today());

    }
}